package com.nit.service.impl;

import com.nit.dto.StockOutDTO;
import com.nit.dto.utils.PaginationDTO;
import com.nit.dto.utils.TableRequestDTO;
import com.nit.dto.utils.TableResponseDTO;
import com.nit.mapper.StockOutMapper;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;
import com.nit.service.StockOutService;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.List;

@Service
public class StoclOutServiceImpl implements StockOutService {
    @Resource
    private StockOutMapper stockOutMapper;

    @Override
    public TableResponseDTO query(TableRequestDTO tableRequestDTO, Long userId) throws Exception {

        Long count = stockOutMapper.count4Table(
                tableRequestDTO.getJsonSearch().get("goodsName"),
                tableRequestDTO.getJsonSearch().get("goodsCate"),
                userId
        );
        List<StockOutDTO> stockOutDTOS = stockOutMapper.list4Table(
                tableRequestDTO.getStart(count),
                tableRequestDTO.getPageSize(),
                tableRequestDTO.getJsonSearch().get("goodsName"),
                tableRequestDTO.getJsonSearch().get("goodsCate"),
                userId
        );
        for (StockOutDTO stockOutDTO : stockOutDTOS) {
            stockOutDTO.setUserName(stockOutMapper.getUserName(stockOutDTO.getUserId()));
        }
        TableResponseDTO tableResponseDTO = new TableResponseDTO(stockOutDTOS,
                new PaginationDTO(tableRequestDTO.getPageSize(),
                        count, tableRequestDTO.getCurrentPage(),
                        String.valueOf(tableRequestDTO.getSearch())));
        return tableResponseDTO;
    }

}
